//
//  Model.m
//  09-省市区
//
//  Created by 韩旭 on 16/7/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "Model.h"

@implementation Model
- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        self.AProvince = dic[@"AProvince"];
        self.Areas = dic[@"Areas"];
        self.ACitys = dic[@"ACitys"];
        _Citys = [NSMutableArray array];
        _Ares = [NSMutableArray array];
    }return self;
}
+ (instancetype)cityModelDic:(NSDictionary *)dic {
    return [[Model alloc] initWithDic:dic];
}

+ (NSArray *)arrGroup {
    NSString *path = @"/Users/hanxu/DLS160710/UI/HomeWork/09-省市区/09-省市区/area.txt";
    NSString *stringPath = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //        根据回车切割字符串
    NSArray *arr = [stringPath componentsSeparatedByString:@"\n"];
    //        存放所有的大字典
    //        1.省数组- 省字典- 市数组- 市字典- 区数组- 区字典
    NSMutableArray *arrAll = [NSMutableArray array];
    for (NSString *namePro in arr) {
        //            取到所有的省（取到所有的省）
        if (![namePro hasPrefix:@" "]) {
            //                创建存放所有省的字典（字典中包含两个字段 name array）
            NSMutableDictionary *MutableDic = [NSMutableDictionary dictionary];
            //                添加ProName字段
            [MutableDic setObject:namePro forKey:@"AProvince"];
            //                创建了省中所有市的数组
            NSMutableArray *arrCity = [NSMutableArray array];
            //                添加数组字段
            [MutableDic setObject:arrCity forKey:@"Citys"];
            //                将创建好的省字典, 放到省数组中
            [arrAll addObject:MutableDic];
            //                取到所有的市（两个空格开头, 但不是四个空格开头）
        } else if ([namePro hasPrefix:@"  "] && ![namePro hasPrefix:@"    "]) {
            //                创建所有市的字典
            NSMutableDictionary *dicCitys = [NSMutableDictionary dictionary];
            NSMutableArray *arrArea = [NSMutableArray array];
            [dicCitys setObject:namePro forKey:@" ACityNames"];
            [dicCitys setObject:arrArea forKey:@"Ares"];
            //           难点：  通过lastObject获取到对应的省的字典
            NSDictionary *dicProvince = [arrAll lastObject];
            //                拿到省字典中对应的市数组
            [[dicProvince objectForKey:@"Citys"] addObject:dicCitys];
        } else {
            //                创建区的字典
            NSMutableDictionary *dicArea = [NSMutableDictionary dictionary];
            [dicArea setObject:namePro forKey:@"AreaNames"];
            //                创建 市字典（name 和 区 array）
            //                通过省字典获取到市数组
            NSMutableDictionary *dicPro = [[[arrAll lastObject] objectForKey:@"Citys"] lastObject];
            //                向市数组添加市字典
            //                最终添加到数组中
            [[dicPro objectForKey:@"Ares"] addObject:dicArea];
        }
    }
    return arrAll;
}



@end
